const crypto = require('crypto');

module.exports = {

    // --------------------------正则部分--------------------------

    // 是否ios
    get isIos() {
        const Reg = /iphone|ipad|ipod/i;
        return Reg.test(this.get('user-agent').toLowerCase());
    },
    // 是否android
    get isAndroid() {
        const Reg = /android/i;
        return Reg.test(this.get('user-agent').toLowerCase());
    },
    // 是否微信
    get isWhchat() {
        const Reg = /micromessenger/i;
        return Reg.test(this.get('user-agent').toLowerCase());
    },
    // 是否微博
    get isWeibo() {
        const Reg = /weibo/i;
        return Reg.test(this.get('user-agent').toLowerCase());
    },
    // 是否淘系
    get isAliapp() {
        const Reg = /aliapp/i;
        return Reg.test(this.get('user-agent').toLowerCase());
    },

    // --------------------------功能部分--------------------------

    monkeyDecode(data) {
        const ctx = this;
        let key = ctx.headers['monkey-key'],
            iv = ctx.headers['monkey-iv'];

        if (key && iv) {
            let src = '';
            const cipher = crypto.createDecipheriv('aes-128-cbc', key, iv);
            src += cipher.update(data, 'hex', 'utf8');
            src += cipher.final('utf8');
            return src;
        } else {
            return data;
        }

    }
}